package cn.mymall.entity.mall;

import cn.mymall.constant.MallConstant;
import cn.mymall.util.DateUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("mall_sku")
public class Sku extends Model {
	private static final long serialVersionUID = -7814850674025960984L;
	/**
	 * 商品SKU
	 */
	@TableId(value = "id",type = IdType.AUTO)
	private Integer id;
	private Integer version;						//更新版本
	private Integer productId;						//商品id
	private String name;							//售卖属性1
	private BigDecimal sellPrice;						//售价
	private BigDecimal costPrice;						//成本价
	private Integer initStock;						//库存
	private Integer sold;							//已售库存
	private String status;							//SKU状态
	private Timestamp addTime;
	private Integer maxBuyNum;						//最大购买数量
	private Long vendorId;							//供应商ID

	public Sku(){
		this.version = 0;
		this.initStock=0;
		this.sold = 0;
		this.maxBuyNum = 99;
		this.addTime = DateUtil.getCurFullTimestamp();
		this.status = MallConstant.SKU_STATUS_OFF_SALE;
	}

	public Sku(Integer productId){
		this();
		this.productId = productId;
	}

	public void addSold(int qty){
		this.sold = this.sold + qty;
	}
	public void addStock(int qty){
		this.initStock = this.initStock + qty;
	}

	public boolean hasBooking(){
		return hasOnSale() && hasStock();
	}
	public boolean hasOnSale(){
		return sellPrice!=null &&
				costPrice!=null &&
				StringUtils.equals(MallConstant.SKU_STATUS_ON_SALE, this.status);
	}
	public boolean hasOffSale(){
		return StringUtils.equals(MallConstant.SKU_STATUS_OFF_SALE, this.status);
	}
	public boolean hasStock(){
		return this.gainStock()>0;
	}
	public String getStockText(){
		if(this.gainStock()<=0){
			return "售罄";
		}
		if(this.gainStock()>=1 && this.gainStock()<=20){
			return "紧张";
		}
		return "充裕";
	}
	public int gainStock() {
		return initStock - sold;
	}

}
